﻿# -*- coding: utf-8 -*-

from django.db import models
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.contrib.sites.managers import CurrentSiteManager
from django.conf import settings

from datetime import datetime

class Menu(models.Model):

    name = models.CharField(u'Name', max_length=100)
    slug = models.CharField(u'Slug', max_length=100)
    site = models.ForeignKey(Site, default=settings.SITE_ID, editable=False)
    
    objects = models.Manager()
    site_objects = CurrentSiteManager('site')

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = u'Menu'
        verbose_name_plural = u'Menu'
        ordering = ['name']
        unique_together = (('slug', 'site'),)

class MenuItem(models.Model):

    parent_menu = models.ForeignKey(Menu,related_name='items')
    parent_item = models.ForeignKey('MenuItem', null=True, blank=True)
    name = models.CharField(u'Name', max_length=100)
    link = models.CharField(u'Link', max_length=255)
    order = models.PositiveSmallIntegerField(u'Order', db_index=True, default=0)
    always_visible = models.BooleanField(u'Always Visible')

    def __unicode__(self):
        return self.name

    _always_visible = None
    def get_always_visible(self):
        if self._always_visible is None:
            self._always_visible = self.menuitem_set.filter(always_visible=True)
        return self._always_visible

    _children = None
    def get_children(self):
        if self._children is None:
            self._children = self.menuitem_set.filter(always_visible=False)
        return self._children

    class Meta:
        verbose_name = u'Item de menu'
        verbose_name_plural = u'Item de menu'
        ordering = ['order', 'name']
